import { createRouter, createWebHistory } from "vue-router";
import JwtService from "@/core/services/JwtService.js";

const routes = [
  {
    path: "/",
    name: "login",
    component: () => import("@/views/login/Index.vue"),
  },
  {
    path: "/signup",
    name: "signup",
    component: () => import("@/views/signup/Index.vue"),
  },
  {
    path: "/user-settings",
    name: "user-settings",
    component: () => import("@/views/user-settings/Index.vue"),
    meta: {
      //路由的自定义字段，bcn用来显示面包屑，role用来限制角色。
      BCN: "学生中心-用户设置",
      requiresRole: "student",
    },
  },
  {
    path: "/course-selection",
    name: "course-selection",
    component: () => import("@/views/course-selection/Index.vue"),
    meta: {
      BCN: "选课系统-选课",
      requiresRole: "student",
    },
  },
  {
    path: "/dormitory-management",
    name: "dormitory-management",
    component: () => import("@/views/dormitory-management/Index.vue"),
    meta: {
      BCN: "宿舍系统-宿舍管理",
      requiresRole: "student",
    },
  },
  {
    path: "/school-profile",
    name: "school-profile",
    component: () => import("@/views/school-profile/Index.vue"),
    meta: {
      BCN: "学校信息-学校简介",
      requiresRole: "student",
    },
  },
  {
    path: "/school-calendar",
    name: "school-calendar",
    component: () => import("@/views/school-calendar/Index.vue"),
    meta: {
      BCN: "学校信息-校历",
      requiresRole: "student",
    },
  },
  {
    path: "/school-bus-schedule",
    name: "school-bus-schedule",
    component: () => import("@/views/school-bus-schedule/Index.vue"),
    meta: {
      BCN: "学校信息-校车时间表",
      requiresRole: "student",
    },
  },
  {
    path: "/school-notice",
    name: "school-notice",
    component: () => import("@/views/school-notice/Index.vue"),
    meta: {
      BCN: "学校通知-学校通知",
      requiresRole: "student",
    },
  },
  {
    path: "/school-notice/:id",
    name: "school-notice-detail",
    component: () => import("@/views/school-notice/Detail.vue"),
    meta: {
      BCN: "学校通知-学校通知-学校通知详情",
      requiresRole: "student",
    },
  },
  {
    path: "/online-consultation",
    name: "online-consultation",
    component: () => import("@/views/online-consultation/Index.vue"),
    meta: {
      BCN: "问题咨询-在线咨询",
      requiresRole: "student",
    },
  },
  {
    path: "/FAQ",
    name: "FAQ",
    component: () => import("@/views/FAQ/Index.vue"),
    meta: {
      BCN: "问题咨询-常见问题",
      requiresRole: "student",
    },
  },
  {
    path: "/home",
    name: "home",
    component: () => import("@/views/home/Index.vue"),
    meta: {
      BCN: "首页",
      requiresRole: "administrator",
    },
  },
  {
    path: "/course",
    name: "course",
    component: () => import("@/views/course/Index.vue"),
    meta: {
      BCN: "课程管理",
      requiresRole: "administrator",
    },
  },
  {
    path: "/dormitory",
    name: "dormitory",
    component: () => import("@/views/dormitory/Index.vue"),
    meta: {
      BCN: "宿舍管理",
      requiresRole: "administrator",
    },
  },
  {
    path: "/student_manage",
    name: "student_manage",
    component: () => import("@/views/student_manage/Index.vue"),
    meta: {
      BCN: "学生管理",
      requiresRole: "administrator",
    },
  },
  {
    path: "/publish_notice",
    name: "publish_notice",
    component: () => import("@/views/publish_notice/Index.vue"),
    meta: {
      BCN: "发布通知",
      requiresRole: "administrator",
    },
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const userRole = localStorage.getItem("userRole"); // 从 localStorage 中获取用户角色

  if (to.matched.some((record) => record.meta.requiresRole)) {
    const requiredRole = to.meta.requiresRole;

    if (userRole === requiredRole) {
      if (!JwtService.getToken()) {
        next("/"); // 用户未登录，重定向到登录页面
      } else {
        next(); // 用户已登录，允许访问
      }
    } else {
      next("/"); // 用户角色不匹配，重定向到登录页面
    }
  } else {
    next(); // 没有角色要求，允许访问
  }
});

export default router;
